package yygh.order.Controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yygh.common.result.Result;
import yygh.common.result.ResultCodeEnum;
import yygh.order.Service.PaymentService;
import yygh.order.Service.WeixinService;

import java.util.Map;

@RestController
@RequestMapping("/api/order/weixin")
public class WeixinController {


    @Autowired
    WeixinService weixinService;
    @Autowired
    PaymentService paymentService;

    //微信订单接口1：生成支付二维码
    @GetMapping("createNative/{orderId}")
    public Result creatNative(@PathVariable Long orderId){
        Map map= weixinService.creatNative(orderId);
        return  Result.ok(map);
    }


    //微信订单接口2：查询订单状态
    @GetMapping("queryPayStatus/{orderId}")
    public Result queryPayStatus(@PathVariable Long orderId) {
        //利用微信接口查询订单状态
        Map<String,String> resultMap=weixinService.queryPayStatus(orderId);

        if (resultMap==null){
            return  Result.fail().message("支付错误");
        }
        if ("SUCCESS".equals(resultMap.get("trade_state"))){
            //生成订单编码
            String out_trade_no = resultMap.get("out_trade_no");
            //调用支付的接口PaymentService更新支付的状态
            paymentService.paySuccess(out_trade_no,resultMap);
            return  Result.ok().message("支付成功");
        }
        return Result.ok().message("支付中");
    }





}
